# Maintainer: fauxpark <fauxpark@gmail.com>

_realname=mkvtoolnix
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-cli"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-gui")
pkgver=73.0.0
pkgrel=1
pkgdesc="Set of tools to create, edit and inspect Matroska files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://mkvtoolnix.download/'
license=('GPLv2')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libebml"
         "${MINGW_PACKAGE_PREFIX}-libmatroska"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-qt6-base")
makedepends=("po4a"
             "zsh"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmark"
             "${MINGW_PACKAGE_PREFIX}-docbook-xsl"
             "${MINGW_PACKAGE_PREFIX}-imagemagick"
             "${MINGW_PACKAGE_PREFIX}-libvorbis"
             "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-ruby"
             "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://mkvtoolnix.download/sources/mkvtoolnix-${pkgver}.tar.xz{,.sig})
sha256sums=('f31a129723571b46a974bc5d57d73733c1245ee429afd6ddaf274038e94e2280'
            'SKIP')
validpgpkeys=('D9199745B0545F2E8197062B0F92290A445B9007') # Moritz Bunkus <moritz@bunkus.org>
noextract=(mkvtoolnix-${pkgver}.tar.xz)

prepare() {
  cd "${srcdir}"

  plain "Extracting mkvtoolnix-${pkgver}.tar.xz due to symlink(s) without pre-existing target(s)"
  tar -xJf ${srcdir}/mkvtoolnix-${pkgver}.tar.xz -C ${srcdir} || true
  # Do it again to fix the symlinks (as the target files didn't exist yet)
  tar -xJf ${srcdir}/mkvtoolnix-${pkgver}.tar.xz -C ${srcdir} || true
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}" \
    --enable-qt6=yes \
    --enable-qt5=no \
    --disable-update-check

  rake
}

package_mkvtoolnix-cli() {
  cd "${srcdir}/${_realname}-${pkgver}"

  depends+=("${MINGW_PACKAGE_PREFIX}-libogg"
            "${MINGW_PACKAGE_PREFIX}-libvorbis"
            "${MINGW_PACKAGE_PREFIX}-zlib")

  rake DESTDIR="${pkgdir}" install

  rm -rf ${pkgdir}${MINGW_PREFIX}/share/{applications,icons,metainfo,mime,mkvtoolnix}
  find "${pkgdir}${MINGW_PREFIX}" -name mkvtoolnix-gui* -delete
}

package_mkvtoolnix-gui() {
  cd "${srcdir}/${_realname}-${pkgver}"

  depends+=("${MINGW_PACKAGE_PREFIX}-cmark"
            "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
            "${MINGW_PACKAGE_PREFIX}-mkvtoolnix-cli"
            "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
            "${MINGW_PACKAGE_PREFIX}-qt6-svg")

  rake DESTDIR="${pkgdir}" install

  rm -rf ${pkgdir}${MINGW_PREFIX}/share/locale
  for t in mkv{extract,info,merge,propedit}; do
    find "${pkgdir}${MINGW_PREFIX}" -name $t* -delete
  done
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
